package com.qingge.springboot.controller;


import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qingge.springboot.entity.Dingdan;
import com.qingge.springboot.mapper.AddressMapper;
import com.qingge.springboot.mapper.DingdanMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.List;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.qingge.springboot.common.Result;

import com.qingge.springboot.service.IAddressService;
import com.qingge.springboot.entity.Address;

import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 格温不受影响
 * @since 2022-05-02
 */
@RestController
@RequestMapping("/address")
public class AddressController {

    @Resource
    private IAddressService addressService;

    @Autowired
    private AddressMapper addressMapper;

    @Autowired
    private DingdanMapper dingdanMapper;

    // 新增或者更新
    @PostMapping
    public Result save(@RequestBody Address address) {
        address.setAddress(address.getAddress().replace("(默认地址)", ""));
        address.setFullAdd(address.getFullAdd().replace("(默认地址)", ""));
        // 更新地址
        if (address.getId() != null) {
            // 更新订单中的地址
            QueryWrapper<Dingdan> wrapper = new QueryWrapper<>();
            wrapper.eq("uid", address.getUid()).
                    eq("address_id", address.getId());
            List<Dingdan> dingdans = dingdanMapper.selectList(wrapper);
            for (Dingdan dingdan : dingdans) {
                dingdan.setSjrname(address.getName());
                dingdan.setSjrphone(address.getPhone());
                dingdan.setSjraddress(address.getFullAdd());
                dingdanMapper.updateById(dingdan);
            }
        }
        // 判断是否更换默认地址
        if (address.getIsDefault() == 1) {
            QueryWrapper<Address> wrapper = new QueryWrapper<>();
            wrapper.eq("uid", address.getUid()).
                    eq("is_default", 1);
            Address one = addressMapper.selectOne(wrapper);
            // 如果已存在不同的默认地址，就更换新默认地址
            if (one != null && !one.getId().equals(address.getId())) {
                one.setIsDefault(0);
                addressMapper.updateById(one);
            }
        }
        addressService.saveOrUpdate(address);
        return Result.success();
    }
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id) {
        addressService.removeById(id);
        return Result.success();
    }


    @PostMapping("/del/batch")
    public Result deleteBatch(@RequestBody List<Integer> ids) {
        addressService.removeByIds(ids);
        return Result.success();
    }

    @GetMapping("/getOne")
    public Result findOne(@RequestParam Integer id) {
        return Result.success(addressService.getById(id));
    }

    @GetMapping("/getDefault")
    public Result getDefault(@RequestParam Integer uid) {
        QueryWrapper<Address> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("uid",uid).
                eq("is_default", 1);
        Address one = addressMapper.selectOne(queryWrapper);
        if (one != null) {
            one.setFullAdd(one.getFullAdd()+"(默认地址)");
        } else {
            one = addressMapper.getMaxUseTimesAdd(uid);
        }
        return Result.success(one);
    }

    @GetMapping("/page")
    public Result findPage(@RequestParam(required = false,defaultValue = "1") Integer pageNum,
                           @RequestParam(required = false,defaultValue = "10") Integer pageSize,
                           @RequestParam(required = false) String address,
                           @RequestParam Integer uid) {
        QueryWrapper<Address> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("is_default","use_times", "id")
                .like(address != null,"address",address)
                .eq("uid",uid);
        Page<Address> page = addressService.page(new Page<>(pageNum, pageSize), queryWrapper);
        List<Address> records = page.getRecords();
        for (Address add : records) {
            if (add.getIsDefault() == 1) {
                add.setAddress(add.getAddress()+"(默认地址)");
                add.setFullAdd(add.getFullAdd()+"(默认地址)");
            }
        }
        page.setRecords(records);
        return Result.success(page);
    }

}

